package com.wiken.orderservice.rocketMQ.consumer;

import com.wiken.orderservice.entity.Order;
import com.wiken.orderservice.service.impl.OrderServiceImpl;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.exception.DefaultException;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "order", consumerGroup = "order_group")
public class OrderExpireConsumer implements RocketMQListener<String> {

    @Autowired
    private OrderServiceImpl orderService;

    @Override
    public void onMessage(String message) {
        String orderId = message;
        System.out.println("接收到延时消息");

        //查看orderId的状态，
        Order order = orderService.getById(orderId);

        //如果状态仍然是未支付，那么将这个订单状态设置为过期，
        if(order.getStatus().equals(1)){
            order.setStatus(3);
            boolean success = orderService.updateById(order);

            if(!success) throw new DefaultException(Code.ERROR_SET_ORDER_EXPIRE, "更新订单状态失败");
        }
        //如果已支付就return
        else return;
    }
}
